package com.ray.service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.ray.bean.CompanyMaterialTypeBean;
import com.ray.bean.DatatablesViewPageBean;
import com.ray.exception.ServiceException;
import com.ray.mapper.CompanyMaterialTypeMapper;
import com.ray.pojo.MyUser;
import com.ray.util.RequestUtil;
import com.ray.util.ReturnStringUtil;

/**
 * 公司物料类型配置表逻辑层
* <p>Title: CompanyMaterialTypeService</p>  
* <p>Description: </p>  
* @author 李斌  
* @date 2018年10月8日
 */
@Service
public class CompanyMaterialTypeService {
	
	@Autowired
	CompanyMaterialTypeMapper companyMaterialTypeMapper;
	
	/**
	 * 获取公司物料类型配置表
	* @date 2018年10月8日 下午2:31:14 
	* @author 李斌
	* @return DatatablesViewPageBean<CompanyMaterialTypeBean> 
	* @throws
	 */
	public DatatablesViewPageBean<CompanyMaterialTypeBean> getCompanyMaterialTypeListByPage(Map<String, String> map) {
		DatatablesViewPageBean<CompanyMaterialTypeBean> datatablesViewPageBean = new DatatablesViewPageBean<CompanyMaterialTypeBean>();
		List<CompanyMaterialTypeBean> companyMaterialTypeList = companyMaterialTypeMapper.getCompanyMaterialTypeListData(map);
		int count = companyMaterialTypeMapper.getCompanyMaterialTypeListCount(map);
		datatablesViewPageBean.setiTotalDisplayRecords(count);
		datatablesViewPageBean.setiTotalRecords(count);
		datatablesViewPageBean.setReturnData(companyMaterialTypeList);
		return datatablesViewPageBean;
	}
	
	/**
	 * 新增物料类型
	* @date 2018年10月9日 上午9:01:19 
	* @author 李斌
	* @return void 
	* @throws
	 */
	public void addCompanyMaterialType(Map<String, String> map) {
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		map.put("createTime", simpleDateFormat.format(new Date()));
		CompanyMaterialTypeBean companyMaterialTypeBean = new CompanyMaterialTypeBean(map);
		checkMaterialType(companyMaterialTypeBean);
		int row = companyMaterialTypeMapper.addCompanyMaterialType(companyMaterialTypeBean);
		Assert.isTrue(row>0, "新增物料类型失败");
		int isShowRow = companyMaterialTypeMapper.addCompanyMaterialTypeShow(companyMaterialTypeBean);
		Assert.isTrue(isShowRow>0, "新增物料类型失败");
	}
	
	/**
	 * 获取有效的物料类型名称
	* @date 2018年10月9日 上午10:27:37 
	* @author 李斌
	* @return List<String> 
	* @throws
	 */
	public List<String> getCompanyMaterialTypeList(Map<String, String> map) {
		return companyMaterialTypeMapper.getCompanyMaterialTypeList(map);
	}
	
	public List<String> getAllCompanyMaterialTypeList(Map<String, String> map){
		return companyMaterialTypeMapper.getAllCompanyMaterialTypeList(map);
	}
	
	/**
	 * 校验物料类型是否存在
	* @date 2018年10月9日 上午10:41:21 
	* @author 李斌
	* @return void 
	* @throws
	 */
	public void checkMaterialType(CompanyMaterialTypeBean companyMaterialTypeBean) {
		Map<String,String> uniqueMap = new HashMap<String,String>();
		uniqueMap.put("loginCompany", companyMaterialTypeBean.getCompany());
		uniqueMap.put("materialType", companyMaterialTypeBean.getMaterialType());
		List<CompanyMaterialTypeBean> companyMaterialTypeBeans = companyMaterialTypeMapper.getCompanyMaterialTypeListData(uniqueMap);
		Assert.isTrue(companyMaterialTypeBeans==null || companyMaterialTypeBeans.size()<1, ReturnStringUtil.MATERIAL_ALERADY_HAVE_EXCEPTION);
	}
	
	/**
	 * 修改物料类型
	* @date 2018年10月9日 上午11:18:18 
	* @author 李斌
	* @return void 
	* @throws
	 */
	public void updateCompanyMaterialType(Map<String, String> map) {
		Assert.notNull(map.get("id"), "物料类型id为空");
		Assert.notNull(map.get("materialType"), "物料类型为空");
		Assert.notNull(map.get("isReadColor"), "是否读颜色为空");
		Assert.notNull(map.get("isSelfAdded"), "是否为公司自己添加为空");
		Assert.notNull(map.get("defaultWarehouse"), "仓位");
		Assert.notNull(map.get("createOrderIsShow"), "制单页面是否显示为空");
		Assert.isTrue(Integer.parseInt(map.get("isSelfAdded"))==1, "公共物料类型不可修改");
		int status = Integer.parseInt(map.get("status"));
		String id = map.get("id");
		//如果是修改状态为使用
		if(status==1) {
			Map<String,String> uniqueMap = new HashMap<String,String>();
			uniqueMap.put("loginCompany", map.get("loginCompany"));
			uniqueMap.put("materialType", map.get("materialType"));
			uniqueMap.put("status",map.get("status"));
			List<CompanyMaterialTypeBean> companyMaterialTypeBeans = companyMaterialTypeMapper.getCompanyMaterialTypeListData(uniqueMap);;
			for(CompanyMaterialTypeBean companyMaterialTypeBean:companyMaterialTypeBeans) {
				if(!id.equals(companyMaterialTypeBean.getId())) {
					throw new ServiceException("该物料类型已存在");
				}
			}
		}
		int row = companyMaterialTypeMapper.updateCompanyMaterialType(map);
		Assert.isTrue(row>0,"修改物料类型失败");
		CompanyMaterialTypeBean companyMaterialTypeBean = new CompanyMaterialTypeBean(map);
		if(companyMaterialTypeBean.getIsSelfAdded()==2) {
			companyMaterialTypeBean.setCompany(null);
			companyMaterialTypeBean.setDefaultWarehouse(null);
		}
		CompanyMaterialTypeBean companyMaterialTypeShow =  this.getCompanyMaterialTypeShow(companyMaterialTypeBean);
		if(companyMaterialTypeShow !=null) {
			int isShowRow = companyMaterialTypeMapper.updateCompanyMaterialTypeShow(map);
			Assert.isTrue(isShowRow>0,"修改物料类型失败");
		}else {
			int isShowRow = companyMaterialTypeMapper.addCompanyMaterialTypeShow(companyMaterialTypeBean);
			Assert.isTrue(isShowRow>0, "修改物料类型失败");
		}
	
	}
	
	/**
	 * 通过物料类型获取是否读颜色
	* @date 2018年10月9日 下午3:04:45 
	* @author 李斌
	* @return CompanyMaterialTypeBean 
	* @throws
	 */
	public CompanyMaterialTypeBean getCompanyMaterialTypeByMateiralType(Map<String,String> map) {
		//获取公司类型
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
		MyUser user = RequestUtil.getLoginUser(request);
		map.put("loginCompanyType", user.getCompanyInfo().getType()+"");
		List<CompanyMaterialTypeBean> companyMaterialTypeBeans = companyMaterialTypeMapper.getCompanyMaterialTypeListData(map);
		/*Assert.isTrue(companyMaterialTypeBeans !=null && companyMaterialTypeBeans.size()==1, "该物料类型配置信息为空或者多条");*/
		return companyMaterialTypeBeans.get(0);
	}
	
	/**
	 * 查询物料类型是否显示
	 * @param companyMaterialTypeBean
	 * @return
	 */
	public CompanyMaterialTypeBean getCompanyMaterialTypeShow(CompanyMaterialTypeBean companyMaterialTypeBean) {
		Assert.notNull(companyMaterialTypeBean, "物料类型信息为空");
		Assert.notNull(companyMaterialTypeBean.getMaterialType(), "物料类型为空");
		if(companyMaterialTypeBean.getIsSelfAdded().intValue()==1) {
			Assert.notNull(companyMaterialTypeBean.getCompany(),"公司名为空");
			Assert.notNull(companyMaterialTypeBean.getDefaultWarehouse(),"仓位为空");
		}
		CompanyMaterialTypeBean companyMaterialTypeShow =  companyMaterialTypeMapper.getCompanyMaterialTypeShow(companyMaterialTypeBean);
		return companyMaterialTypeShow;
	}
	
	/**
	 * 查询物料类型不分页
	 * @param map
	 * @return
	 */
	public List<CompanyMaterialTypeBean> getCompanyMaterialTypeListData(Map<String, String> map) {
		return companyMaterialTypeMapper.getCompanyMaterialTypeListData(map);
	}

}
